package com.rt.easyjava.base.biz1.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.easyjava.base.biz1.bean.TmptUserSetting;
import com.rt.easyjava.base.biz1.dto.TmptUserSettingDto;
import com.rt.easyjava.base.biz1.mapper.TmptUserSettingMapper;
import com.rt.easyjava.base.biz1.service.intf.TmptUserSettingService;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.easyjava.common.utils.others.DateTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 *  服务实现类
 * @author wuxiaoyong
 * @since 2020-03-18
 */
@Service(value = "tmptUserSettingService")
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class TmptUserSettingServiceImpl extends ServiceImpl<TmptUserSettingMapper, TmptUserSetting> implements TmptUserSettingService {
    private final Logger logger = LoggerFactory.getLogger(TmptUserSettingServiceImpl.class);

    @Autowired
    private MyMapper mapper;
    @Resource
    private TmptUserSettingMapper tmptUserSettingMapper;

    // ======================================================================
    // ========== 以下使用的是 Mybatis-plus 实现 ==============================
    // ======================================================================

    /**
    * 根据createManId、name查询，有则修改，无则创建
    * @param tmptUserSettingDto
    * @return
    */
    @Override
    public Result<TmptUserSettingDto> createOrUpdate(TmptUserSettingDto tmptUserSettingDto) {
        Result<TmptUserSettingDto> result = new Result<>();
        // query
        TmptUserSetting tmptUserSettingParam = new TmptUserSetting();
        tmptUserSettingParam.setCreatemanId(tmptUserSettingDto.getCreatemanId());
        tmptUserSettingParam.setName(tmptUserSettingDto.getName());
        tmptUserSettingParam.setDeleteflag(0);
        QueryWrapper queryWrapper = new QueryWrapper(tmptUserSettingParam);
        TmptUserSetting tmptUserSetting = tmptUserSettingMapper.selectOne(queryWrapper);
        // create
        if (tmptUserSetting == null) {
            TmptUserSetting tmptUserSettingNew = mapper.map(tmptUserSettingDto, TmptUserSetting.class);
            tmptUserSettingNew.insert();
        }
        // update
        else {
            tmptUserSetting.setCreatedate(null);
            tmptUserSetting.setCreateman(null);
            tmptUserSetting.setCreatemanId(null);
            tmptUserSetting.setCompanyId(null);
            tmptUserSetting.setModifyman(tmptUserSettingDto.getModifyman());
            tmptUserSetting.setModifymanId(tmptUserSettingDto.getModifymanId());
            tmptUserSetting.setModifydate(DateTool.getCurTimestamp());
            tmptUserSetting.setValue(tmptUserSettingDto.getValue());
            tmptUserSetting.updateById();
        }

        return result.success();
    }

    /**
    * 根据createManId、name查询，有则修改，无则创建
    * @param tmptUserSettingDto
    * @return
    */
    @Override
    public Result<TmptUserSettingDto> createOrUpdateGlobalUnique(TmptUserSettingDto tmptUserSettingDto) {
        Result<TmptUserSettingDto> result = new Result<>();
        // query
        TmptUserSetting tmptUserSettingParam = new TmptUserSetting();
        tmptUserSettingParam.setName(tmptUserSettingDto.getName());
        tmptUserSettingParam.setDeleteflag(0);
        QueryWrapper queryWrapper = new QueryWrapper(tmptUserSettingParam);
        TmptUserSetting tmptUserSetting = tmptUserSettingMapper.selectOne(queryWrapper);
        // create
        if (tmptUserSetting == null) {
            TmptUserSetting tmptUserSettingNew = mapper.map(tmptUserSettingDto, TmptUserSetting.class);
            tmptUserSettingNew.insert();
        }
        // update
        else {
            tmptUserSetting.setCreatedate(null);
            tmptUserSetting.setCreateman(null);
            tmptUserSetting.setCreatemanId(null);
            tmptUserSetting.setCompanyId(null);
            tmptUserSetting.setModifyman(tmptUserSettingDto.getModifyman());
            tmptUserSetting.setModifymanId(tmptUserSettingDto.getModifymanId());
            tmptUserSetting.setModifydate(DateTool.getCurTimestamp());
            tmptUserSetting.setValue(tmptUserSettingDto.getValue());
            tmptUserSetting.updateById();
        }

        return result.success();
    }

    /**
     * create a object
     * @param tmptUserSettingDto
     * @return
     */
    @Override
    public Result<TmptUserSettingDto> create(TmptUserSettingDto tmptUserSettingDto) {
        Result<TmptUserSettingDto> result = new Result<>();

        // dto to entity
        TmptUserSetting tmptUserSetting = mapper.map(tmptUserSettingDto, TmptUserSetting.class);

        tmptUserSettingMapper.insert(tmptUserSetting);

        return result.success();
    }

    /**
    * update a object by Id
    * @param tmptUserSettingDto
    * @param updateWrapper
    * @return
    */
    @Override
    public Result<TmptUserSettingDto> update(TmptUserSettingDto tmptUserSettingDto, Wrapper updateWrapper) {
        Result<TmptUserSettingDto> result = new Result<>();

        // dto to entity
        TmptUserSetting tmptUserSetting = mapper.map(tmptUserSettingDto, TmptUserSetting.class);

        tmptUserSettingMapper.update(tmptUserSetting, updateWrapper);

        return result.success();
    }

    /**
    * 分页
    * @param page
    * @param queryWrapper
    * @return
    */
    public Result<TmptUserSettingDto> page(Page<TmptUserSetting> page, Wrapper<TmptUserSetting> queryWrapper){
        Result<TmptUserSettingDto> result = new Result<>();

        // mapper
        IPage<TmptUserSetting> iPage = tmptUserSettingMapper.selectPage(page, queryWrapper);
        // 查询结果转换成DTO
        List<TmptUserSettingDto> tmptUserSettingDtoList= mapper.maplist(iPage.getRecords(), TmptUserSettingDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptUserSettingDtoList);

        return result.success();
    }

    /**
    * select all by queryWrapper
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptUserSettingDto> selectAll(Wrapper<TmptUserSetting> queryWrapper) {
        Result<TmptUserSettingDto> result = new Result<>();

        List<TmptUserSetting> list = tmptUserSettingMapper.selectList(queryWrapper);

        List<TmptUserSettingDto> dtoList = mapper.maplist(list, TmptUserSettingDto.class);

        return result.setList(dtoList).success();
    }

    /**
    * select one by id
    * @param id
    * @return
    */
    @Override
    public Result<TmptUserSettingDto> selectById(Long id) {
        Result<TmptUserSettingDto> result = new Result<>();

        TmptUserSetting tmptUserSetting = tmptUserSettingMapper.selectById(id);

        TmptUserSettingDto tmptUserSettingdto = mapper.map(tmptUserSetting, TmptUserSettingDto.class);

        return result.setObj(tmptUserSettingdto).success();
    }




    // ======================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现 =========================
    // ======================================================================

    /**
    * 分页查询：mybatis plus 自定义sql分页查询
    * @param page
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptUserSettingDto> myPage(Page<TmptUserSetting> page, Wrapper<TmptUserSetting> queryWrapper) {
        Result<TmptUserSettingDto> result = new Result();

        IPage<TmptUserSetting> iPage = tmptUserSettingMapper.myPage(page, queryWrapper);

        //查询结果转换成DTO
        List<TmptUserSettingDto> tmptUserSettingDtoList = mapper.maplist(iPage.getRecords(), TmptUserSettingDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptUserSettingDtoList);

        return result.success();
    }

    @Override
    public Result<TmptUserSettingDto> mySelectBy(Wrapper<TmptUserSetting> queryWrapper) {
        Result<TmptUserSettingDto> result = new Result();

        List<TmptUserSetting> list = tmptUserSettingMapper.mySelectBy(queryWrapper);
        List<TmptUserSettingDto> dtoList = mapper.maplist(list, TmptUserSettingDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<TmptUserSettingDto> mySelectById(long id) {
        Result<TmptUserSettingDto> result = new Result();

        TmptUserSetting tmptUserSetting = tmptUserSettingMapper.mySelectById(id);
        TmptUserSettingDto dto = mapper.map(tmptUserSetting,TmptUserSettingDto.class);
        result.setObj(dto);

        return result.success();
    }


    /**
    * 返回ID值
    */
    @Override
    public Result<TmptUserSettingDto> mySave(TmptUserSettingDto tmptUserSettingDto){
        Result<TmptUserSettingDto> result = new Result();

        //封装数据
        TmptUserSetting tmptUserSetting = mapper.map(tmptUserSettingDto, TmptUserSetting.class);
        tmptUserSetting.setCreatedate(DateTool.getCurTimestamp());

        //调用mapper
        long i = tmptUserSettingMapper.myInsert(tmptUserSetting);
        if(i==0){
            return result.fault("插入失败");
        }

        TmptUserSettingDto dto = mapper.map(tmptUserSetting,TmptUserSettingDto.class);
        result.setObj(dto);

        return result.success();
    }

    /**
    返回ID值
    */
    @Override
    public Result<TmptUserSettingDto> myEdit(TmptUserSettingDto tmptUserSettingDto){
        Result<TmptUserSettingDto> result = new Result();

        //封装数据
        TmptUserSetting tmptUserSetting = mapper.map(tmptUserSettingDto, TmptUserSetting.class);
        tmptUserSetting.setModifydate(DateTool.getCurTimestamp());

        //调用mapper
        long i = tmptUserSettingMapper.myUpdateById(tmptUserSetting);
        if(i==0){
            return result.fault("编辑数据失败");
        }

        TmptUserSettingDto dto = mapper.map(tmptUserSetting,TmptUserSettingDto.class);
        result.setObj(dto);

        return result.success();
    }

    /**
    伪删除记录，返回ID值
    */
    @Override
    public Result myDeleteById(long id,String adminMan){
        Result result = new Result();

        TmptUserSetting tmptUserSetting = new TmptUserSetting();
        tmptUserSetting.setId(id);
        tmptUserSetting.setModifydate(DateTool.getCurTimestamp());
        tmptUserSetting.setModifyman(adminMan);

        //调用mapper
        long i = tmptUserSettingMapper.myDeleteById(tmptUserSetting);
        if(i==0){
            return result.fault("删除数据失败");
        }

        return result.success();
    }



}
